package cn.cs.mathgo.app.server.authentication;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyLogoutHandler implements LogoutHandler {
    //springmvc启动会自动注册一个ObjectMapper
    @Autowired
    private ObjectMapper objectMapper;
    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        response.setStatus(200);
        response.setContentType("application/json;charset=UTF-8");
        try {
            addCrossHeader(response);
            response.getWriter().write("/authentication/require");//值返回异常信息
            response.getWriter().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void addCrossHeader(HttpServletResponse response) {
        response.addHeader("Access-Control-Allow-Origin",
                "*");
        response.setHeader("Access-Control-Allow-Headers",
                "Origin, X-Requested-With, Content-Type, Accept, Cookie, X-Token");
        response.setHeader("Access-Control-Allow-Methods",
                "GET, POST, PUT, DELETE, OPTIONS");
        response.addHeader("Access-Control-Max-Age", "1728000");
        response.addHeader("Access-Control-Allow-Credentials", "true");
        //logger.info("跨域");
    }

}
